package main.demonutils.io;

import main.model.hexisland.HexIsland;
import main.model.hexisland.tile.Tile;
import main.model.hexisland.tile.TileFactory;
import main.model.hexisland.tile.TileTree;
import main.model.hexisland.tile.TileType;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: gabe
 * Date: 01/07/13
 * Time: 18:31
 * To change this template use File | Settings | File Templates.
 */
public class IslandIOStrategyCSV{

    public static final char SAND = 's';
    public static final char WATER = 'w';
    public static final char ROCK = 'r';
    public static final char GRASS = 'g';
    public static final char TREE = 't';
    public static final char UNKNOWN = 'u';
    public static final char DELIMITER = ';';
    public static final char VAR_MARKER = ',';
    public static final String FILE_TYPE = "DELIMITED_ISLAND";

    public boolean writeHexIsland(HexIsland hexIsland , File islandFile) {
        try {
            if (!islandFile.exists()) {
                if(islandFile.createNewFile()){
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(islandFile));

                    bufferedWriter.write(FILE_TYPE + DELIMITER + hexIsland.getQSize()+ DELIMITER +hexIsland.getRSize());
                    for(int q = 0 ; q < hexIsland.getQSize() ; q++){
                        bufferedWriter.write("\n");
                        for(int r = 0 ; r < hexIsland.getRSize() ; r++){
                            switch(hexIsland.getTileType(q,r)){
                                case WATER: bufferedWriter.write(WATER + DELIMITER); break;
                                case SAND: bufferedWriter.write(SAND + DELIMITER); break;
                                case ROCK: bufferedWriter.write(ROCK + DELIMITER); break;
                                case GRASS:bufferedWriter.write(GRASS + DELIMITER); break;
                                case TREE:
                                    Tile tile = hexIsland.getTile(q,r);
                                    int value = 0;
                                    if(tile instanceof TileTree){
                                        value = ((TileTree) tile).getTree();
                                    }
                                    bufferedWriter.write(TREE + VAR_MARKER + value + VAR_MARKER + TileTree.getTreeMax() + DELIMITER);
                                    break;
                                default: bufferedWriter.write(UNKNOWN + DELIMITER); break;

                            }
                        }
                    }
                    bufferedWriter.close();
                    return true;
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

    public boolean readHexIsland(HexIsland hexIsland , File islandFile){
        try {
            FileReader fileReader = new FileReader(islandFile);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String buffer = bufferedReader.readLine();
            if(buffer != null){
                String [] bufferSplit = buffer.split(""+DELIMITER);
                if(bufferSplit.length == 3
                        && bufferSplit[0].equals(FILE_TYPE)){
                    int qsize = Integer.parseInt(bufferSplit[1]);
                    int rsize = Integer.parseInt(bufferSplit[2]);
                    hexIsland.clear(qsize , rsize);
                } else throw new IOException("Bad header line.");
                buffer = bufferedReader.readLine();
                int q = 0;
                while(buffer != null){
                    if(q > hexIsland.getQSize())
                        throw new IOException("Column exceeds stated size.");

                    bufferSplit = buffer.split(""+DELIMITER);

                    if(bufferSplit.length > hexIsland.getRSize())
                        throw new IOException("Row exceeds stated size.");

                    for(int r = 0 ; r < bufferSplit.length ; r ++){
                        //System.out.println("reading "+bufferSplit[r]);
                        switch(bufferSplit[r].charAt(0)){
                            case WATER:
                                hexIsland.setTile(q,r, TileFactory.newTile(TileType.WATER));
                                break;
                            case SAND:
                                hexIsland.setTile(q,r, TileFactory.newTile(TileType.SAND));
                                break;
                            case ROCK:
                                hexIsland.setTile(q,r, TileFactory.newTile(TileType.ROCK));
                                break;
                            case GRASS:
                                hexIsland.setTile(q,r, TileFactory.newTile(TileType.GRASS));
                                break;
                            case TREE:
                                String[] treeSplit = bufferSplit[r].split(""+VAR_MARKER);
                                hexIsland.setTile(q,r, new TileTree(Integer.parseInt(treeSplit[2])));
                                break;
                        }
                    }
                    q ++;
                    buffer = bufferedReader.readLine();
                }
            }
            return true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e){
            e.printStackTrace();
        }
        return false;
    }
}
